home *** CD-ROM | disk | FTP | other *** search
- /*
- * (c) Copyright 1992 by Panagiotis Tsirigotis
- * All rights reserved. The file named COPYRIGHT specifies the terms
- * and conditions for redistribution.
- */
-
- static char RCSid[] = "$Id: log.c,v 5.2 1992/11/17 06:34:06 panos Exp $" ;
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/wait.h>
- #include <syslog.h>
- #include <time.h>
-
- #include "sio.h"
- #include "str.h"
-
- #include "defs.h"
- #include "access.h"
- #include "logoptions.h"
- #include "server.h"
- #include "state.h"
-
- void msg() ;
-
- #define CHECK_LOGSIZE TRUE
- #define DONT_CHECK_LOGSIZE FALSE
-
- #define LOGBUF_SIZE 1024
-
-
- char *inet_ntoa() ;
- time_t time() ;
-
-
- /*
- * This function writes log records of the form:
- *
- * START: service pid [from_address] [time]
- *
- */
- void log_success( serp )
- struct server *serp ;
- {
- char buf[ LOGBUF_SIZE ] ;
- int bufsize ;
- struct service *sp = SERVER_SERVICE( serp ) ;
- register struct service_config *scp = CONF( sp ) ;
- register int len ;
- register int cc ;
- int log_common() ;
-
- if ( ! SVC_IS_LOGGING( sp ) || M_ARE_ALL_CLEAR( scp->log_on_success ) )
- return ;
-
- bufsize = sizeof( buf ) ;
- len = 0 ;
-
- cc = strx_nprint( buf, bufsize, "START: %s", scp->id ) ;
- len += cc ;
- bufsize -= cc ;
-
- if ( M_IS_SET( scp->log_on_success, LO_PID ) )
- {
- cc = strx_nprint( &buf[ len ], bufsize, " pid=%d", serp->pid ) ;
- len += cc ;
- bufsize -= cc ;
- }
-
- cc = log_common( &scp->log_on_failure, &buf[len], bufsize,
- SERVER_CONNECTION( serp ) ) ;
- len += cc ;
- bufsize -= cc ;
-
- xlog_write( SDATA( sp )->log_handle, buf, len, XLOG_NOFLAGS ) ;
- }
-
-
- /*
- * This function writes log records of the form:
- *
- * FAIL: service failure-type [from_address] [time]
- *
- */
- void log_failure( access_failure, sp, cp )
- access_e access_failure ;
- register struct service *sp ;
- connection_s *cp ;
- {
- char buf[ LOGBUF_SIZE ] ;
- int bufsize ;
- register struct service_config *scp = CONF( sp ) ;
- register int len = 0 ;
- register int cc ;
- int log_common() ;
- void remote_user_logging() ;
-
- if ( ! SVC_IS_LOGGING( sp ) || M_ARE_ALL_CLEAR( scp->log_on_failure ) )
- return ;
-
- bufsize = sizeof( buf ) ;
- cc = strx_nprint( buf, bufsize, "FAIL: %s", scp->id ) ;
- len += cc ;
- bufsize -= cc ;
-
- switch ( access_failure )
- {
- case AC_FORK:
- cc = strx_nprint( &buf[ len ], bufsize, " fork" ) ;
- break ;
-
- case AC_TIME:
- cc = strx_nprint( &buf[ len ], bufsize, " time" ) ;
- break ;
-
- case AC_ADDRESS:
- cc = strx_nprint( &buf[ len ], bufsize, " address" ) ;
- break ;
-
- case AC_SERVICE_LIMIT:
- cc = strx_nprint( &buf[ len ], bufsize, " service_limit" ) ;
- break ;
-
- case AC_PROCESS_LIMIT:
- cc = strx_nprint( &buf[ len ], bufsize, " process_limit" ) ;
- break ;
-
- default:
- cc = strx_nprint( &buf[ len ], bufsize, " BAD_ENUM_VALUE" ) ;
- }
- len += cc ;
- bufsize -= cc ;
-
- cc = log_common( &scp->log_on_failure, &buf[ len ], bufsize, cp ) ;
- len += cc ;
- bufsize -= cc ;
-
- xlog_write( SDATA( sp )->log_handle, buf, len, XLOG_NOFLAGS ) ;
- }
-
-
-
- PRIVATE int log_common( logmask, buf, bufsize, cp )
- mask_t *logmask ;
- char *buf ;
- int bufsize ;
- connection_s *cp ;
- {
- register int len = 0 ;
- int cc ;
-
- if ( M_IS_SET( *logmask, LO_HOST ) )
- {
- cc = strx_nprint( &buf[ len ], bufsize, " from=%s",
- conn_address( cp ) ?
- inet_ntoa( conn_address( cp )->sin_addr ) : "UNKNOWN" ) ;
- len += cc ;
- bufsize -= cc ;
- }
-
- if ( M_IS_SET( *logmask, LO_TIME ) )
- {
- time_t current_time ;
- struct tm *tmp ;
-
- (void) time( ¤t_time ) ;
- tmp = localtime( ¤t_time ) ;
- cc = strx_nprint( &buf[ len ], bufsize, " time=%d/%d/%d@%02d:%02d:%02d",
- tmp->tm_year, tmp->tm_mon+1, tmp->tm_mday,
- tmp->tm_hour, tmp->tm_min, tmp->tm_sec ) ;
- len += cc ;
- bufsize -= cc ;
- }
- return( len ) ;
- }
-
-
-
- void log_exit( serp )
- register struct server *serp ;
- {
- char buf[ LOGBUF_SIZE ] ;
- int bufsize ;
- register int cc ;
- register struct service *sp = SERVER_SERVICE( serp ) ;
- register struct service_config *scp = CONF( sp ) ;
- register int len ;
- char *func = "log_exit" ;
-
- if ( ! SVC_IS_LOGGING( sp ) )
- return ;
-
- bufsize = sizeof( buf ) ;
- len = 0 ;
-
- cc = strx_nprint( buf, bufsize, "EXIT: %s", scp->id ) ;
- bufsize -= cc ;
- len += cc ;
-
- /*
- * If the EXIT flag was used, log the exit status or the signal that
- * killed the process. We assume that these are the only reasons
- * for process termination.
- */
- if ( M_IS_SET( scp->log_on_success, LO_EXIT ) )
- {
- int num ;
- char *s ;
-
- if ( PROC_EXITED( serp->exit_status ) )
- {
- s = "status" ;
- num = PROC_EXITSTATUS( serp->exit_status ) ;
- }
- else if ( PROC_SIGNALED( serp->exit_status ) )
- {
- s = "signal" ;
- num = PROC_TERMSIG( serp->exit_status ) ;
- }
- else
- {
- msg( LOG_ERR, func, "Bad exit status" ) ;
- s = NULL ;
- }
-
- if ( s )
- {
- cc = strx_nprint( &buf[ len ], bufsize, " %s=%d", s, num ) ;
- len += cc ;
- bufsize -= cc ;
- }
- }
-
- if ( M_IS_SET( scp->log_on_success, LO_PID ) )
- {
- cc = strx_nprint( &buf[ len ], bufsize, " pid=%d", serp->pid ) ;
- len += cc ;
- bufsize -= cc ;
- }
-
- if ( M_IS_SET( scp->log_on_success, LO_DURATION ) )
- {
- time_t current_time ;
-
- (void) time( ¤t_time ) ;
- cc = strx_nprint( &buf[ len ], bufsize, " duration=%d(sec)",
- current_time - serp->start_time ) ;
- len += cc ;
- bufsize -= cc ;
- }
- xlog_write( SDATA( sp )->log_handle, buf, len, XLOG_NOFLAGS ) ;
- }
-
-
-
- /*
- * Used by other parts of xinetd that want to log something without
- * going through the proper channels (i.e. log_{success,failure} and log_exit)
- */
- /* VARARGS3 */
- void logprint( sp, line_id, fmt, va_alist )
- register struct service *sp ;
- char *line_id ;
- char *fmt ;
- va_dcl
- {
- struct service_config *scp = CONF( sp ) ;
- char buf[ LOGBUF_SIZE ] ;
- int bufsize = sizeof( buf ) ;
- int len ;
- int cc ;
- va_list ap ;
-
- if ( ! SVC_IS_LOGGING( sp ) )
- return ;
-
- len = strx_nprint( buf, bufsize, "%s: %s ", line_id, scp->id ) ;
- va_start( ap ) ;
- cc = strx_nprintv( &buf[ len ], bufsize, fmt, ap ) ;
- va_end( ap ) ;
- xlog_write( SDATA( sp )->log_handle, buf, len+cc, XLOG_NO_SIZECHECK ) ;
- }
-
-